function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return decodeURI(r[2]); return null; //返回参数值
}

$(function () {
    'use strict';

    $(document).on("pageInit",'#list',function(e, id, page){
        //预先加载20条
        //addItems(itemsPerLoad, 0);
        var obj = $(page).find('.page-index')
        var cate_id = getUrlParam('cate_id');
        addItems(1)
        //console.log(obj);
    });
    var loading = false;
    // 最多可加载的条目
    var maxItems = 100;

    // 每次加载添加多少条目
    var itemsPerLoad = 20;
    var lastIndex = 1;

    function addItems(page) {
        // 生成新条目的HTML
        var html = '';
        var cate_id = $('#card_list').attr('cate_id');
        var url = './'+cate_id+'/list/?page=';
        $.getJSON(url + page, function(remoteData){
            lastIndex = remoteData.to;
            maxItems = remoteData.total;
            $(remoteData.data.data).forEach(function(item, index, array){
                var gifBool = typeof(item.content.gif_src) != "undefined"?true:false;
                var gifSrc = gifBool?item.content.gif_src:'';
                var img_html = '';
                //多图片遍历
                if(Array.isArray(item.content.src) == true) {
                    $(item.content.src).forEach(function(item, index, array){
                        img_html += '<div valign="bottom" style="margin-top:1px;" class="card-header color-white no-border no-padding">' +
                                        '<img class=\'card-cover '+(gifBool?'gif':'no-gif')+'\' gif_src="'+ gifSrc +'" src="'+ item +'" alt="">' +
                                    '</div>';
                    });
                }else {
                    //单图片
                    img_html = '<div valign="bottom" class="card-header color-white no-border no-padding">' +
                                    '<img id="' + item.unique_sign + '" class=\'card-cover '+(gifBool?'gif':'no-gif')+'\' gif_src="'+ gifSrc +'" src="'+ item.content.src +'" alt="">' +
                                //判断是否显示play
                                (gifBool?'<div class="gif-mask" ><div img_id="'+ item.unique_sign +'" class="play">play</div></div>':'')+
                                '</div>';
                }
                html += '<div class="card demo-card-header-pic">' +
                            img_html +
                            '<div class="card-content">' +
                                '<div class="card-content-inner">' +
                                    '<p class="color-gray">发表于 2015/01/15</p>' +
                                '</div>' +
                            '</div>' +
                            '<div class="card-footer interflow">' +
                                '<a href="#" class="button" url="praise" img-id="' + item.id + '">赞<span class="interflow-num">'+ item.praise +'</span></a>' +
                                '<a href="#" class="button" url="disdain" img-id="' + item.id + '">踩<span class="interflow-num">'+ item.disdain +'</span></a>' +
                            '</div>' +
                        '</div>';
            });
            // 添加新条目
            $('#card_list').append(html);
        });
    }
    //加载gif动图
    $(document).on('click','.play',function(){
        var img_id = $(this).attr('img_id');
        $('#'+img_id).attr('src',$('#'+img_id).attr('gif_src'));
        $(this).parent().remove();
    });
    $(document).on('click','.no-gif',function(){
        var img_src = $(this).attr('src');
        var imgobj = $.photoBrowser({
            photos : [img_src],
            type: 'popup'
        });
        imgobj.open();
    });
    var page = 2;

    // 注册'infinite'事件处理函数
    $(document).on('infinite', '.infinite-scroll-bottom',function() {
        // 如果正在加载，则退出
        if (loading) return;

        // 设置flag
        loading = true;

        if (lastIndex >= maxItems) {
            // 加载完毕，则注销无限加载事件，以防不必要的加载
            $.detachInfiniteScroll($('.infinite-scroll'));
            // 删除加载提示符
            $('.infinite-scroll-preloader').remove();
            return;
        }
        // 重置加载flag
        loading = false;
        // 添加新条目
        addItems(page);
        // 更新最后加载的序号
        page++;
        //容器发生改变,如果是js滚动，需要刷新滚动
        $.refreshScroller();
    });

    //女优模块
    $(document).on("pageInit",'#av_rank',function(e, id, page){
        var obj = $(page).find('.page-index')
        console.log(12321);
        $.getJSON('./artist/rank', function(remoteData){
            $(remoteData.data.rank).forEach(function(item, index, array){
                var html = '<a href="./production?artist='+ encodeURI(item.artist) +'"><div class="card">' +
                    '<div class="card-content">' +
                    '<div class="card-content-inner" style="text-align:center;"><img src="' + item.avatar + '" />' +
                    '</div>' +
                    '</div>' +
                    '<div class="card-footer">' + item.artist + '</div>' +
                    '</div></a>';
                console.log(html);
                obj.append(html)
                //console.log(index, item)
            })
        })
        //console.log(obj);
    });
    $(document).on("pageInit",'#production',function(e, id, page){
        var obj = $(page).find('.page-index')
        var artist = getUrlParam('artist');
        $.getJSON('./artist/'+ artist +'/production', function(remoteData){
            $(remoteData.data.last_production).forEach(function(item, index, array){
                var html = '<div class="card">' +
                    '<div class="card-header">'+ item[0] +'</div>' +
                    '<div class="card-content">' +
                    '<div class="card-content-inner" style="text-align:center;"><img src="//' + item[2] + '" />' +
                    '</div>' +
                    '</div>' +
                    '<div class="card-footer">' + item[1] + '</div>' +
                    '</div>';
                //console.log(html);
                obj.append(html)
                //console.log(index, item)
            })
        })
        //console.log(obj);
    });

    $(document).on("click",'.interflow .button',function(e){
        var id = $(this).attr('img-id');
        var url = $(this).attr('url');
        var obj = $(this);
        var cookieName = 'img-'+id+'-'+url;
        if($.fn.cookie(cookieName) == 'ok') {
            var text = url == 'praise'?'点赞':'鄙视';
            $.toast('已'+ text +'过啦~');
            return false;
        }
        $.getJSON('./list/'+ url + '?id='+id, function(remoteData) {
            if(remoteData.code == 1) {
                var num = obj.find('.interflow-num').text();
                obj.find('.interflow-num').text(parseInt(num)+1);
                $.fn.cookie(cookieName,'ok',{ expires : 7 });
            }
            //显示提示
            $.toast(remoteData.msg);
        });
    });

    $.init();
});